package com.hzk.controller;

import com.hzk.plug_in.plugin_param;
import com.hzk.pojo.UserBasic;
import com.hzk.service.IUserBasicService;
import com.hzk.utils.IDCardUtil;
import com.hzk.utils.MD5Util;
import com.hzk.utils.ageutils;
import com.hzk.vo.RespBean;
import com.hzk.vo.RespBeanEnum;
import com.hzk.vo.loginVo;
import com.hzk.vo.userVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jack
 * @since 2022-02-25
 */

@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {
    @Autowired
    IUserBasicService iUserBasicService;
    @Autowired
    RedisTemplate redisTemplate;
    //注册
    @ResponseBody
    @RequestMapping("/plugin")
    public RespBean plugin(plugin_param pluginParam) {
        boolean idNumber = IDCardUtil.isIDNumber(pluginParam.getUserid());
        if (!idNumber){
        return RespBean.error(RespBeanEnum.IDCARD_ERROR);
    }
    UserBasic byId = iUserBasicService.getById(pluginParam.getPhone());
        if (byId!=null){
        return RespBean.error(RespBeanEnum.PLUGIN_ERROR);
    }
    UserBasic userBasic = new UserBasic();
    //手机号不存在就直接存进数据库
        userBasic.setPassword(MD5Util.formPassToDBPass(pluginParam.getPASSWORD(),"1a2b3c"));
        userBasic.setUsername(pluginParam.getUsername());
        userBasic.setId(Long.parseLong(pluginParam.getPhone()));
        userBasic.setAge(Integer.parseInt(ageutils.evaluate(String.valueOf(pluginParam.getUserid()))));
        userBasic.setSalt("1a2b3c");
        userBasic.setRegisterDate(new Date());
        userBasic.setTaomi(200);
        iUserBasicService.save(userBasic);

        return RespBean.success();
}

    @RequestMapping("/toLogin")
    public String LoLogin() {
        return "login";
    }
    //登录
    @RequestMapping("/doLogin")
    @ResponseBody
    public RespBean DoLogin(@Valid loginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        return iUserBasicService.login(loginVo, request, response);
    }

    //退出账号
    @RequestMapping("/doCanCell")
    @ResponseBody
    public RespBean cancell(@Valid loginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        return iUserBasicService.login(loginVo, request, response);
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public RespBean getuser(HttpServletRequest request, HttpServletResponse response,UserBasic user) {
        /*if (userBasic==null)
        String token = request.getHeader("token");
        UserBasic user = (UserBasic) redisTemplate.opsForValue().get("UserBasic:"+token);*/
        if (user == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        userVo userVo = new userVo();
        userVo.setUsername(user.getUsername());
        userVo.setMobile(String.valueOf(user.getId()));
        userVo.setCardMoney(user.getCardMoney());
        userVo.setTaomi(user.getTaomi());
        return RespBean.success(user);
    }
}


